package com.zyk.scaffold.user.controller.tenantuser;


import com.zyk.scaffold.common.basic.BasicConstants;
import com.zyk.scaffold.core.domain.PageFilter;
import com.zyk.scaffold.core.domain.PageResult;
import com.zyk.scaffold.core.domain.Result;
import com.zyk.scaffold.user.service.TenantUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 租户-用户关系 前端控制器
 * </p>
 *
 * @author zhouyk
 * @since 2022-11-04
 */
@Api(tags = "租户-用户-API")
@RestController
@RequestMapping("/tenant-user")
public class TenantUserController {

    @Autowired
    private TenantUserService tenantUserService;

    @ApiOperation("新增用户")
    @PostMapping("/newUser")
    public Result<Boolean> newUser(@RequestBody @Validated TenantUserNewRequ tenantUserNewRequ,
                                   @RequestHeader(BasicConstants.HEADER_USER_ID) Long userId,
                                   @RequestHeader(BasicConstants.HEADER_TENANT_CODE) String tenantCode) {
        Boolean aBoolean = tenantUserService.addTenantUser(tenantUserNewRequ, userId, tenantCode);
        return Result.ok(aBoolean);
    }

    @ApiOperation("分页过滤用户")
    @PostMapping("/pageFilterUser")
    public Result<PageResult<TenantUserInfosResp>> pageFilterUser(@RequestBody PageFilter<TenantUserPageFilterRequ> pageFilter,
                                                                  @RequestHeader(BasicConstants.HEADER_TENANT_CODE) String tenantCode) {
        PageResult<TenantUserInfosResp> page = tenantUserService.pageFilterTenantUser(pageFilter, tenantCode);
        return Result.ok(page);
    }

    @ApiOperation("更新用户")
    @PostMapping("/editedUser")
    public Result<Boolean> editedUser(@RequestBody @Validated TenantUserEditedRequ tenantUserEditedRequ,
                                      @RequestHeader(BasicConstants.HEADER_USER_ID) Long userId,
                                      @RequestHeader(BasicConstants.HEADER_TENANT_CODE) String tenantCode){
        Boolean aBoolean = tenantUserService.editedTenantUser(tenantUserEditedRequ, userId, tenantCode);
        return Result.ok(aBoolean);
    }

    @ApiOperation("删除用户")
    @PostMapping("/deletedUser")
    public Result<Boolean> deletedUser(@RequestBody @Validated TenantUserDeletedRequ tenantUserDeletedRequ,
                                       @RequestHeader(BasicConstants.HEADER_USER_ID) Long userId,
                                       @RequestHeader(BasicConstants.HEADER_TENANT_CODE) String tenantCode){
        Boolean aBoolean = tenantUserService.deletedTenantUser(tenantUserDeletedRequ, userId, tenantCode);
        return Result.ok(aBoolean);
    }

    @ApiOperation("禁用用户")
    @PostMapping("/disabledUser")
    public Result<Boolean> disabledUser(@RequestBody @Validated TenantUserDisableRequ tenantUserDisableRequ,
                                        @RequestHeader(BasicConstants.HEADER_USER_ID) Long userId,
                                        @RequestHeader(BasicConstants.HEADER_TENANT_CODE) String tenantCode){
        Boolean aBoolean = tenantUserService.disabledTenantUser(tenantUserDisableRequ, userId, tenantCode);
        return Result.ok(aBoolean);
    }

    @ApiOperation("启用用户")
    @PostMapping("/enabledUser")
    public Result<Boolean> enabledUser(@RequestBody @Validated TenantUserEnableRequ tenantUserEnableRequ,
                                       @RequestHeader(BasicConstants.HEADER_USER_ID) Long userId,
                                       @RequestHeader(BasicConstants.HEADER_TENANT_CODE) String tenantCode){
        Boolean aBoolean = tenantUserService.enabledTenantUser(tenantUserEnableRequ, userId, tenantCode);
        return Result.ok(aBoolean);
    }

    @ApiOperation("用户租户列表")
    @PostMapping("/tenant-list")
    public Result<List<TenantInfoResp>> tenantList(@RequestHeader(BasicConstants.HEADER_USER_ID) Long userId) {
        List<TenantInfoResp> tenantInfoResps = tenantUserService.tenantList(userId);
        return Result.ok(tenantInfoResps);
    }

}
